
MyQuests.BagUpdated = nil;



--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:MapLocationCheck()
  local questCount = table.getn(self.db.char.QuestLog);

  --GetZoneText() == objective.name or GetSubZoneText() == objective.name

  for i=1, questCount, 1 do
    local tmpQuest = self.db.char.QuestLog[i];
    local objCount = table.getn(tmpQuest.objectives);
    
    for j=1, objCount, 1 do
      local tmpObj = tmpQuest.objectives[j];
      if (tmpObj.type == "Explore") then
        if (MyQuests.QuestObjectives["Explore"]:OnUpdate(tmpObj)) then
          MyQuests:CheckQuestComplete(tmpQuest);
        end
      end      
    end
  end    
end
MyQuests:ScheduleRepeatingEvent(MyQuests.MapLocationCheck, 5, MyQuests);



--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:MyQuestsTargetBadge_OnClick()
  if(UnitName("target") ~= nil) then
    getglobal("MyQuests_GreetingText"):SetText(MQL["Transcribing Quests"]);
    
    HideUIPanel(QuestFrameGreetingPanel);
    ShowUIPanel(QuestFrame);
    SetPortraitTexture(QuestFramePortrait, "target");
    getglobal("QuestFrameNpcNameText"):SetText(UnitName("target"));
    ShowUIPanel(MyQuests_GreetingPanel);

    if (UnitName("target") == UnitName("player")) then
      self:TriggerEvent("MYQUESTS_MSG_LIST_RECEIVED", UnitName("target"), self.db.char.QuestDiary);
    else
      self:SendCommMessage("WHISPER", UnitName("target"), "RequestQuestListing");  
    end
    
  end
end


--[[ ********************************************************************* --]]
--[[ Event: MYQUESTS_MSG_QUEST_TITLES                                      --]]
--[[   Fired when current target is a MyQuests user.                       --]]
--[[ Arguments:                                                            --]]
--[[   sender  - player name triggering the event (current target).        --]]
--[[   message - table containing a list of available quest titles and     --]]
--[[     ids.                                                              --]]
--[[ ********************************************************************* --]]
function MyQuests:MYQUESTS_MSG_PING_REPLY(sender, message)
  if (UnitName("target") == sender) then
    MyQuests_TargetBadge:Show();

    local questCount = tonumber(message);
    if (questCount == 0) then
      MyQuests_TargetBadgeButtonTexture:SetTexture("Interface\\AddOns\\MyQuests\\images\\badge2.tga");
    else
      MyQuests_TargetBadgeButtonTexture:SetTexture("Interface\\AddOns\\MyQuests\\images\\badge1.tga");
    end
  end
end

--[[ ********************************************************************* --]]
--[[ Event: MYQUESTS_MSG_LIST_RECEIVED                                     --]]
--[[   Fired when receiving a full cache of available quests from the      --]]
--[[     current target.                                                   --]]
--[[ Arguments:                                                            --]]
--[[   sender  - player name triggering the event (current target).        --]]
--[[   message - table containing a list of available quest titles and     --]]
--[[     ids.                                                              --]]
--[[ ********************************************************************* --]]
function MyQuests:MYQUESTS_MSG_LIST_RECEIVED(sender, message)
  MyQuests.QuestCache = {};
  MyQuests.QuestCache.sender = sender;

  -- filter out non-repeatables
  for i=1, table.getn(message), 1 do
  end

  MyQuests.QuestCache.quests = message;

  HideUIPanel(MyQuests_GreetingPanel);
  ShowUIPanel(QuestFrameGreetingPanel);
end

--[[ ********************************************************************* --]]
--[[ Event: MYQUESTS_MSG_QUEST_INFO                                        --]]
--[[   Fired when current target send specific quest information.          --]]
--[[ Arguments:                                                            --]]
--[[   sender  - player name triggering the event (current target).        --]]
--[[   message - table containing requested quest information.             --]]
--[[ ********************************************************************* --]]
function MyQuests:MYQUESTS_MSG_QUEST_INFO(sender, message)
  -- received after clicking on a quest title and getting a reply
  -- show the quest screen
  -- show quest information
end

--[[ ********************************************************************* --]]
--[[ Event: MYQUESTS_TURNIN_REQUEST                                        --]]
--[[   Fired when a player has requested a quest turnin to you.            --]]
--[[ Arguments:                                                            --]]
--[[   sender  - player name triggering the event.                         --]]
--[[   message - id of the quest being turned in.                          --]]
--[[ ********************************************************************* --]]
function MyQuests:MYQUESTS_TURNIN_REQUEST(sender, message)
  -- check if we are accepting turnins
  -- check if this is stored locally already
  
  -- check if any trade needs to be complete
  -- if yes, send trade to target


  -- RIGHT NOW WE JUST ACCEPT THINGS AND MOVE ON (message = quest id)
  if (UnitName("target") == sender) then
    MyQuests:TriggerEvent("MYQUESTS_TURNIN_ACCEPTED", sender, message);
  else
    self:SendCommMessage("WHISPER", sender, "TurninAccepted", message);    
  end
end

--[[ ********************************************************************* --]]
--[[ Event: MYQUESTS_TURNIN_ACCEPTED                                       --]]
--[[   Fired on receiving verification from the quest turnin player that   --]]
--[[     the quest turnin is complete.                                     --]]
--[[ Arguments:                                                            --]]
--[[   sender  - player name triggering the event.                         --]]
--[[   message - id of the quest being turned in.                          --]]
--[[ ********************************************************************* --]]
function MyQuests:MYQUESTS_TURNIN_ACCEPTED(sender, message)
  -- remove id from quest log
  for i=1, table.getn(self.db.char.QuestLog), 1 do
    if (self.db.char.QuestLog[i].id == message) then
      table.remove(self.db.char.QuestLog, i);
    end
  end
  
  -- add quest to quest history
  if (self.db.char.QuestHistory[message]) then
    self.db.char.QuestHistory[message] = self.db.char.QuestHistory[message] + 1;
  else
    self.db.char.QuestHistory[message] = 1;
  end
  
  -- hide the window
  QuestFrame:Hide();
end


--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:ChatFrame_MessageEventHandler(event)
  --MyQuests:Print("ChatFrame_MessageEventHandler = " .. event .. " - " .. arg1);
  
  if (event == "CHAT_MSG_SYSTEM") then
  end

  if (event == "CHAT_MSG_WHISPER") then
    --MyQuests:Debug("CHAT_MSG_WHISPER - " .. arg1);
  end
  
  self.hooks["ChatFrame_MessageEventHandler"](event);
end


--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:FriendsFrame_OnEvent()
  if (event == "WHO_LIST_UPDATE") then
    SetWhoToUI(0);
    
    if (MyQuests.QueuedDuelWho) then
      MyQuests.QueuedDuelWho = nil;

      local count = GetNumWhoResults();
      if (count > 1) then
        -- loop through results until we find a direct match
      end

      local plrname, plrguild, plrlevel, plrrace, plrclass, plrzone, unknown = GetWhoInfo(1)
      
      -- call the complete for duel quests

      --MyQuests:Debug(GetWhoInfo(1));
    else
      self.hooks["FriendsFrame_OnEvent"](event);
    end
  else
    self.hooks["FriendsFrame_OnEvent"](event);
  end  
end

--[[ ********************************************************************* --]]
--[[ Function: GetNumAvailableQuests (overloaded)                          --]]
--[[   Called by WoW when the QuestFrameGreetingPanel is shown.            --]]
--[[ ********************************************************************* --]]
function MyQuests:GetNumAvailableQuests()
  if (MyQuests.QuestCache) then
    local questCount = 0;
    for i=1, table.getn(MyQuests.QuestCache.quests), 1 do
      if (not MyQuests:IsQuestActive(MyQuests.QuestCache.quests[i].id)) then
        questCount = questCount + 1;
      end
    end
    
    return questCount;
  else
    return self.hooks["GetNumAvailableQuests"]();
  end
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:QuestFrame_OnHide()
  MyQuests.QuestCache = nil;
  return self.hooks["QuestFrame_OnHide"]();
end

--[[ ********************************************************************* --]]
--[[ Function: GetNumActiveQuests (overloaded)                             --]]
--[[   Called by WoW when the QuestFrameGreetingPanel is shown.            --]]
--[[ ********************************************************************* --]]
function MyQuests:GetNumActiveQuests()
  if (MyQuests.QuestCache) then
    local questCount = 0;
    for i=1, table.getn(self.db.char.QuestLog), 1 do
      if (self.db.char.QuestLog[i].finishPlayer == MyQuests.QuestCache.sender) then
        questCount = questCount + 1;
      end
    end
    return questCount;
  else
    return self.hooks["GetNumActiveQuests"]();
  end
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetAvailableTitle(id)
  if (MyQuests.QuestCache) then
    local newId = MyQuests:GetAvailableQuestId(id);
    getglobal("QuestTitleButton" .. id).MyQuestsId = MyQuests.QuestCache.quests[newId].id;
    return MyQuests.QuestCache.quests[newId].title;
  end

  return self.hooks["GetAvailableTitle"](id);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetActiveTitle(id)
  if (MyQuests.QuestCache) then
    local count = 0;

    for i=1, table.getn(self.db.char.QuestLog), 1 do
      if(self.db.char.QuestLog[i].finishPlayer == MyQuests.QuestCache.sender) then
        count = count + 1;
        if (count == id) then
          getglobal("QuestTitleButton" .. id).MyQuestsId = MyQuests.QuestCache.quests[id].id;
          return self.db.char.QuestLog[i].title;
        end
      end
    end

    return MyQuests.QuestCache.quests[id].title;
  end

  return self.hooks["GetActiveTitle"](id);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetGreetingText()
  if (MyQuests.QuestCache) then
    if (MyQuests.QuestCache.greeting) then
      return "Replace this with the transfered greeting.";
    else
      return "This is a generic welcome.";
    end
  end

  return self.hooks["GetGreetingText"]();  
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:SelectAvailableQuest(id)
  if (MyQuests.QuestCache) then
    MyQuests.QuestCache.current = MyQuests.QuestCache.quests[MyQuests:GetAvailableQuestId(id)];

    HideUIPanel(QuestFrameGreetingPanel);
    ShowUIPanel(QuestFrameDetailPanel);
  end
  
  return self.hooks["SelectAvailableQuest"](id);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:SelectActiveQuest(id)
  if (MyQuests.QuestCache) then
    local count = 0;

    for i=1, table.getn(self.db.char.QuestLog), 1 do
      if(self.db.char.QuestLog[i].finishPlayer == MyQuests.QuestCache.sender) then
        count = count + 1;
        
        if (count == id) then
          MyQuests.QuestCache.current = self.db.char.QuestLog[i];
          
          HideUIPanel(QuestFrameGreetingPanel);

          if(self.db.char.QuestLog[i].isComplete) then
            ShowUIPanel(QuestFrameRewardPanel);
          else
            ShowUIPanel(QuestFrameProgressPanel);
          end

          return;
        end
      end
    end
  end

  return self.hooks["SelectActiveQuest"](id);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CloseQuest()
  MyQuests.QuestCache = nil;
  return self.hooks["CloseQuest"](id);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CompleteQuest(id)
  if(MyQuests.QuestCache) then
    HideUIPanel(QuestFrameProgressPanel);
    HideUIPanel(QuestFrameRewardPanel);
    return;
  end

  return self.hooks["CompleteQuest"](id);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetTitleText()
  if(MyQuests.QuestCache) then
    return MyQuests.QuestCache.current.title;
  end

  return self.hooks["GetTitleText"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestText()
  if(MyQuests.QuestCache) then
    return MyQuests.QuestCache.current.descText;
  end

  return self.hooks["GetQuestText"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetObjectiveText()
  if(MyQuests.QuestCache) then
    return MyQuests.QuestCache.current.summaryText;
  end

  return self.hooks["GetObjectiveText"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetProgressText()
  if(MyQuests.QuestCache) then
    return MyQuests.QuestCache.current.progressText;
  end

  return self.hooks["GetProgressText"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetRewardText()
  if(MyQuests.QuestCache) then
    return MyQuests.QuestCache.current.completeText;
  end

  return self.hooks["GetRewardText"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetNumQuestRewards()
  return self.hooks["GetNumQuestRewards"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:IsQuestCompletable()
  return self.hooks["IsQuestCompletable"]();
end

--[[ ********************************************************************* --]]
--[[ ... this is where we start the quest validation process.  QuestComplete is just used to close things out --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestReward(id)
  if (MyQuests.QuestCache) then
    if (UnitName("target") == MyQuests.QuestCache.sender) then
      MyQuests:TriggerEvent("MYQUESTS_TURNIN_REQUEST", MyQuests.QuestCache.sender, MyQuests.QuestCache.current["id"]);
    else
      self:SendCommMessage("WHISPER", MyQuests.QuestCache.sender, "TurninRequest", MyQuests.QuestCache.current["id"]);      
    end
  end
  
--[[  
    if(MQBypass_Quest) then
      -- inform the finishPlayer that you've completed the quest
  
      local dataOut = {
        ["action"] = mq.COMM_TURNIN_P_REQUEST,
        ["mqId"] = MQBypass_Quest.id
      };
      mq.SendMessage(dataOut, MQBypass_Quest.finishPlayers[1]);
    end
--]]
  
  return self.hooks["GetQuestReward"](id);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetRewardMoney()
  return self.hooks["GetRewardMoney"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetNumQuestItems()
  return self.hooks["GetNumQuestItems"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:AcceptQuest()
  local entries, quests = GetNumQuestLogEntries();

  if (MyQuests.QuestCache) then
    if (table.getn(self.db.char.QuestLog) >= MAX_QUESTS) then
      MyQuests:Print("Quest Log is full");
      DeclineQuest();
      return;
    end

    -- TODO: check if QuestLog[questGiver] exists
    -- if no, create it
    -- add to QuestLog[questGiver]
    
    --if (not self.db.char.QuestLog[MyQuests.QuestCache.sender]) then
    --  self.db.char.QuestLog[MyQuests.QuestCache.sender] = {};
    --end
    
    table.insert(self.db.char.QuestLog, MyQuests.QuestCache.current);
  
    self:Print("Quest accepted: " .. MyQuests.QuestCache.current.title);

    -- flag the bags to be checked
    MyQuests.BagUpdated = 1;
    
    -- clear the pending quest
    MyQuests.QuestCache = nil;
    
    -- hide the quest frame
    HideUIPanel(QuestFrame);

    self:TriggerEvent("MYQUESTS_LOG_UPDATE");
    
    return;
  end

  return self.hooks["AcceptQuest"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:DeclineQuest()
  if (MyQuests.QuestCache) then
    MyQuests.QuestCache.current = nil;
    HideUIPanel(QuestFrameDetailPanel);
    ShowUIPanel(QuestFrameGreetingPanel);
    return;
  end

  return self.hooks["DeclineQuest"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetRewardSpell()
  if (MyQuests.QuestCache) then
    return nil;
  end
  
  return self.hooks["GetRewardSpell"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetNumQuestLogEntries()
  if (MyQuests.QuestLogBypass) then
    local numQuests = table.getn(self.db.char.QuestLog);
    -- numEntries, numQuests
    return numQuests, numQuests;
  end
  
  return self.hooks["GetNumQuestLogEntries"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetDailyQuestsCompleted(numQuests)
  if (MyQuests.QuestLogBypass) then
    return 0;
  end

  return self.hooks["GetDailyQuestsCompleted"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:SelectQuestLogEntry(questIndex)
  if (MyQuests.QuestLogBypass) then
    MyQuests.QuestLogSelection = questIndex;
    return;
  end

  return self.hooks["SelectQuestLogEntry"](questIndex);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:AbandonQuest()
  if (MyQuests.QuestLogBypass) then
    if (MyQuests.QuestLogSelection) then
      table.remove(self.db.char.QuestLog, MyQuests.QuestLogSelection);
      MyQuests.QuestLogSelection = 0;
    end
    return;
  end
  
  return self.hooks["AbandonQuest"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestLogSelection()
  if (MyQuests.QuestLogBypass) then
    return MyQuests.QuestLogSelection;
  end
  
  return self.hooks["GetQuestLogSelection"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:SetAbandonQuest()
  if (MyQuests.QuestLogBypass) then
    MyQuests.QuestLogAbandon = MyQuests.QuestLogSelection;
    return;
  end
  
  return self.hooks["SetAbandonQuest"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetAbandonQuestName()
  if (MyQuests.QuestLogAbandon) then
    return self.db.char.QuestLog[MyQuests.QuestLogAbandon].title;
  end
  
  return self.hooks["GetAbandonQuestName"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestLogTitle(questIndex)
  if (MyQuests.QuestLogBypass) then
    local numQuests = GetNumQuestLogEntries();
    if (questIndex > numQuests) then
      return;
    end
    
    local tmpQuest = self.db.char.QuestLog[questIndex];
    
    local questTag;
    if (tmpQuest.isFailed) then
      questTag = MQL["Failed"];
    elseif (tmpQuest.isComplete) then
      questTag = MQL["Complete"];
    end
    
    --questLogTitleText, level, questTag, isHeader, isCollapsed, isComplete
    return tmpQuest.title, tmpQuest.level, questTag, nil, nil, tmpQuest.isComplete;
  end

  return self.hooks["GetQuestLogTitle"](questIndex);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestLogQuestText()
  if (MyQuests.QuestLogBypass) then
    return self.db.char.QuestLog[MyQuests.QuestLogSelection].descText,
           self.db.char.QuestLog[MyQuests.QuestLogSelection].summaryText;
  end

  return self.hooks["GetQuestLogQuestText"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestLogTimeLeft()
  if (MyQuests.QuestLogBypass) then
    return self.db.char.QuestLog[MyQuests.QuestLogSelection].timeLeft;
  end
  
  return self.hooks["GetQuestLogTimeLeft"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestLogLeaderBoard(i, questIndex)

  if (MyQuests.QuestLogBypass) then
    local tmpGoal = self.db.char.QuestLog[MyQuests.QuestLogSelection].objectives[i];
    
    return MyQuests.QuestObjectives[tmpGoal.type]:GetLeaderBoard(tmpGoal),
           tmpGoal.type,
           tmpGoal.isComplete;
  end
  
  return self.hooks["GetQuestLogLeaderBoard"](i, questIndex);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestLogRequiredMoney()
  if (MyQuests.QuestLogBypass) then
    return self.db.char.QuestLog[MyQuests.QuestLogSelection].objectiveMoney;
  end

  return self.hooks["GetQuestLogRequiredMoney"];
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetNumQuestLeaderBoards(questIndex)
  if (MyQuests.QuestLogBypass and self.db.char.QuestLog) then
    if (not questIndex) then
      questIndex = MyQuests.QuestLogSelection;
    end
    return table.getn(self.db.char.QuestLog[questIndex].objectives);
  end
  
  return self.hooks["GetNumQuestLeaderBoards"](questIndex);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetQuestLogRewardMoney()
  if (MyQuests.QuestLogBypass) then
    return self.db.char.QuestLog[MyQuests.QuestLogSelection].rewardMoney;
  end
  
  return self.hooks["GetQuestLogRewardMoney"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetNumQuestLogRewards()
  if (MyQuests.QuestLogBypass) then
    if (self.db.char.QuestLog[MyQuests.QuestLogSelection].rewards) then
      return table.getn(self.db.char.QuestLog[MyQuests.QuestLogSelection].rewards);
    else
      return 0;
    end
  end
  
  return self.hooks["GetNumQuestLogRewards"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetNumQuestLogChoices()
  if (MyQuests.QuestLogBypass) then
    if (self.db.char.QuestLog[MyQuests.QuestLogSelection].rewardChoices) then
      return table.getn(self.db.char.QuestLog[MyQuests.QuestLogSelection].rewardChoices);
    else
      return 0;
    end
  end
  
  return self.hooks["GetNumQuestLogChoices"]();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:IsQuestActive(questId)
  for i=1, table.getn(self.db.char.QuestLog), 1 do
    --if (self.db.char.QuestLog[i].finishPlayers[1] == targetChar) then
      if (self.db.char.QuestLog[i].id == questId) then
        return 1;
      end
    --end
  end
  
  return nil;
end

--[[ ********************************************************************* --]]
--[[ Function: IsQuestInHistory                                            --]]
--[[   Checks to see if the quest has already been completed and turned    --]]
--[[   in by the requesting player.                                        --]]
--[[ ********************************************************************* --]]
function MyQuests:IsQuestInHistory()
end


--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:GetAvailableQuestId(id)
  local count = 0;

  for i=1, table.getn(MyQuests.QuestCache.quests), 1 do
    if (not MyQuests:IsQuestActive(MyQuests.QuestCache.quests[i].id)) then
      count = count + 1;
      if (count == id) then
        return i;
      end
    end
  end
  
  return nil;
end









--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:QuestLog_OnShow()
  self.hooks["QuestLog_OnShow"]();
  
  QuestLogFrameTab1:Show();
  QuestLogFrameTab2:Show();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:QuestLog_OnHide()
  QuestLogFrameTab1:Hide();
  QuestLogFrameTab2:Hide();
  
  self.hooks["QuestLog_OnHide"]();
end

--[[ ********************************************************************* --]]
--[[ MyQuests:BAG_UPDATE                                                   --]]
--[[   Called when the contents of the bags updated.  Used to track item   --]]
--[[   counts for quest rewards.                                           --]]
--[[ ********************************************************************* --]]
function MyQuests:BAG_UPDATE()
  if (arg1 >= 0 and arg1 <= 4) then
    MyQuests.BagUpdated = 1;
  end  
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CHAT_MSG_CHANNEL_NOTICE()
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CHAT_MSG_COMBAT_HONOR_GAIN()
  local s, e, creep = string.find(arg1, MQL["CHAT_MSG_COMBAT_HONOR_GAIN"]);

  --if (creep == UnitName("target")) then
    -- this is already done, I think, in the target change.
  --end

  local questCount = table.getn(self.db.char.QuestLog);

  for i=1, questCount, 1 do
    local tmpQuest = self.db.char.QuestLog[i];
    local objCount = table.getn(tmpQuest.objectives);

    for j=1, objCount, 1 do
      local tmpObj = tmpQuest.objectives[j];
      if (tmpObj.type == "PvpAny") then
        MyQuests.QuestObjectives["PvpAny"]:OnUpdate(tmpObj);
      end
      if (tmpObj.type == "PvpClass") then
        MyQuests.QuestObjectives["PvpClass"]:OnUpdate(tmpObj);
      end
      if (tmpObj.type == "PvpGuild") then
        MyQuests.QuestObjectives["PvpGuild"]:OnUpdate(tmpObj);
      end
      if (tmpObj.type == "PvpPlayer") then
        MyQuests.QuestObjectives["PvpPlayer"]:OnUpdate(tmpObj);
      end
      if (tmpObj.type == "PvpRace") then
        MyQuests.QuestObjectives["PvpRace"]:OnUpdate(tmpObj);
      end
    end
    MyQuests:CheckQuestComplete(tmpQuest);
  end
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CHAT_MSG_COMBAT_HOSTILE_DEATH()
  local s, e, creep = string.find(arg1, MQL["REGEX_HOSTILE_SLAIN"]);

  if (creep) then
    local questCount = table.getn(self.db.char.QuestLog);
    
    for i=1, questCount,1 do
      local tmpQuest = self.db.char.QuestLog[i];
      local objCount = table.getn(tmpQuest.objectives);

      for j=1, objCount, 1 do
        local tmpObj = tmpQuest.objectives[j];
        if (creep == tmpObj.name) then
          MyQuests.QuestObjectives["KillMonster"]:OnUpdate(tmpObj);
        end
      end    
      MyQuests:CheckQuestComplete(tmpQuest);
    end
  end

  -- TODO: should we check for this as well?
  --MQL["REGEX_HOSTILE_DEATH"];

end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CHAT_MSG_COMBAT_XP_GAIN()
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CHAT_MSG_TEXT_EMOTE()
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CHAT_MSG_SYSTEM()
  local s, e, winner, loser = string.find(arg1, MQL["REGEX_DUEL"]);
  if (winner and winner == UnitName("player")) then
    MyQuests:Debug("I won a duel against", loser);
    TargetByName(loser);

    local questCount = table.getn(self.db.char.QuestLog);

    for i=1, questCount, 1 do
      local tmpQuest = self.db.char.QuestLog[i];
      local objCount = table.getn(tmpQuest.objectives);

      for j=1, objCount, 1 do
        local tmpObj = tmpQuest.objectives[j];
        if (tmpObj.type == "DuelAny") then
          MyQuests.QuestObjectives["DuelAny"]:OnUpdate(tmpObj);
        end
        if (tmpObj.type == "DuelClass") then
          MyQuests.QuestObjectives["DuelClass"]:OnUpdate(tmpObj);
        end
        if (tmpObj.type == "DuelGuild") then
          MyQuests.QuestObjectives["DuelGuild"]:OnUpdate(tmpObj);
        end
        if (tmpObj.type == "DuelPlayer") then
          MyQuests.QuestObjectives["DuelPlayer"]:OnUpdate(tmpObj);
        end
        if (tmpObj.type == "DuelRace") then
          MyQuests.QuestObjectives["DuelRace"]:OnUpdate(tmpObj);
        end
      end
    end
    MyQuests:CheckQuestComplete(tmpQuest);
  end 
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:PLAYER_DEAD()
  local questCount = table.getn(self.db.char.QuestLog);
  
  for i=1, questCount, 1 do
    if (self.db.char.QuestLog[i].failOnDeath) then
      self.db.char.QuestLog[i].isFailed = 1;
    end
  end    
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:PLAYER_MONEY()
end


--[[ ********************************************************************* --]]
--[[ MyQuests:PLAYER_TARGET_CHANGED                                        --]]
--[[   Called when the target is changed.  Used for to ping players to     --]]
--[[   check for MyQuests installation.                                    --]]
--[[ ********************************************************************* --]]
function MyQuests:PLAYER_TARGET_CHANGED()
  MyQuests_TargetBadge:Hide();

  -- bail out if the target is not in the same faction
  if (UnitFactionGroup("target") ~= UnitFactionGroup("player")) then
    return;
  end

  -- show the badge and exit out if target is self
  if (UnitName("target") == UnitName("player")) then
    --MyQuests_TargetBadge:Show();

    --local tableOut = {};

    local questCount = table.getn(self.db.char.QuestDiary);
    --for i=1, questCount, 1 do
    --  tableOut[i] = {};
    --  tableOut[i].id = self.db.char.QuestDiary[i].id;
    --  tableOut[i].title = self.db.char.QuestDiary[i].title;
    --end

    self:TriggerEvent("MYQUESTS_MSG_PING_REPLY", UnitName("player"), questCount);
  else
    -- send a ping to see if the target is using MyQuests
    self:SendCommMessage("ZONE", "Ping", UnitName("target"));  
  end
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:QUEST_LOG_UPDATE()
end

--[[ ********************************************************************* --]]
--[[ MyQuests:ZONE_CHANGED                                                 --]]
--[[   Called when the enters a new zone or sub-zone.  Used for updating   --]]
--[[   exploration quest objectives.                                       --]]
--[[ ********************************************************************* --]]
function MyQuests:ZONE_CHANGED()
  local questCount = table.getn(self.db.char.QuestLog);
  
  for i=1, questCount, 1 do
    local tmpQuest = self.db.char.QuestLog[i];
    local objCount = table.getn(tmpQuest.objectives);
    
    for j=1, objCount, 1 do
      local tmpObj = tmpQuest.objectives[j];
      if (tmpObj.type == "Explore") then
        -- update state of explore objective
      end
    end
    MyQuests:CheckQuestComplete(tmpQuest);
  end
end

